home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / ksocketbase.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  22.4 KB  |  763 lines

  1. /*  -*- C++ -*-
  2.  *  Copyright (C) 2003,2005 Thiago Macieira <thiago.macieira@kdemail.net>
  3.  *
  4.  *
  5.  *  Permission is hereby granted, free of charge, to any person obtaining
  6.  *  a copy of this software and associated documentation files (the
  7.  *  "Software"), to deal in the Software without restriction, including
  8.  *  without limitation the rights to use, copy, modify, merge, publish,
  9.  *  distribute, sublicense, and/or sell copies of the Software, and to
  10.  *  permit persons to whom the Software is furnished to do so, subject to
  11.  *  the following conditions:
  12.  *
  13.  *  The above copyright notice and this permission notice shall be included 
  14.  *  in all copies or substantial portions of the Software.
  15.  *
  16.  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17.  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18.  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19.  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20.  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21.  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22.  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25. /*
  26.  * Even before our #ifdef, clean up the namespace
  27.  */
  28. #ifdef socket
  29. #undef socket
  30. #endif
  31.  
  32. #ifdef bind
  33. #undef bind
  34. #endif
  35.  
  36. #ifdef listen
  37. #undef listen
  38. #endif
  39.  
  40. #ifdef connect
  41. #undef connect
  42. #endif
  43.  
  44. #ifdef accept
  45. #undef accept
  46. #endif
  47.  
  48. #ifdef getpeername
  49. #undef getpeername
  50. #endif
  51.  
  52. #ifdef getsockname
  53. #undef getsockname
  54. #endif
  55.  
  56. #ifndef KSOCKETBASE_H
  57. #define KSOCKETBASE_H
  58.  
  59. #include <qiodevice.h>
  60. #include <qstring.h>
  61.  
  62. #include "ksocketaddress.h"
  63. #include <kdelibs_export.h>
  64.  
  65. /*
  66.  * This is extending QIODevice's error codes
  67.  *
  68.  * According to qiodevice.h, the last error is IO_UnspecifiedError
  69.  * These errors will never occur in functions declared in QIODevice
  70.  * (except open, but you shouldn't call open)
  71.  */
  72. #define IO_ListenError        (IO_UnspecifiedError+1)
  73. #define IO_AcceptError        (IO_UnspecifiedError+2)
  74. #define IO_LookupError        (IO_UnspecifiedError+3)
  75. #define IO_SocketCreateError    (IO_UnspecifiedError+4)
  76. #define IO_BindError        (IO_UnspecifiedError+5)
  77.  
  78. class QMutex;
  79.  
  80. namespace KNetwork {
  81.  
  82. class KResolverEntry;
  83. class KSocketDevice;
  84.  
  85. class KSocketBasePrivate;
  86. /** @class KSocketBase ksocketbase.h ksocketbase.h
  87.  *  @brief Basic socket functionality.
  88.  *
  89.  * This class provides the basic socket functionlity for descended classes.
  90.  * Socket classes are thread-safe and provide a recursive mutex should it be 
  91.  * needed.
  92.  *
  93.  * @note This class is abstract.
  94.  *
  95.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  96.  */
  97. class KDECORE_EXPORT KSocketBase
  98. {
  99. public:
  100.   /**
  101.    * Possible socket options.
  102.    *
  103.    * These are the options that may be set on a socket:
  104.    *  - Blocking: whether the socket shall operate in blocking
  105.    *    or non-blocking mode. This flag defaults to on.
  106.    *    See @ref setBlocking.
  107.    *  - AddressReusable: whether the address used by this socket will
  108.    *    be available for reuse by other sockets. This flag defaults to off.
  109.    *    See @ref setAddressReuseable.
  110.    *  - IPv6Only: whether an IPv6 socket will accept IPv4 connections
  111.    *    through a mapped address. This flag defaults to off.
  112.    *    See @ref setIPv6Only.
  113.    *  - KeepAlive: whether TCP should send keepalive probes when a connection
  114.    *    has gone idle for far too long.
  115.    *  - Broadcast: whether this socket is allowed to send broadcast packets
  116.    *    and will receive packets sent to broadcast.
  117.    */
  118.   enum SocketOptions
  119.     {
  120.       Blocking = 0x01,
  121.       AddressReuseable = 0x02,
  122.       IPv6Only = 0x04,
  123.       Keepalive = 0x08,
  124.       Broadcast = 0x10
  125.     };
  126.  
  127.   /**
  128.    * Possible socket error codes.
  129.    *
  130.    * This is a list of possible error conditions that socket classes may
  131.    * be expected to find.
  132.    *
  133.    * - NoError: no error has been detected
  134.    * - LookupFailure: if a name lookup has failed
  135.    * - AddressInUse: address is already in use
  136.    * - AlreadyBound: cannot bind again
  137.    * - AlreadyCreated: cannot recreate the socket
  138.    * - NotBound: operation required socket to be bound and it isn't
  139.    * - NotCreated: operation required socket to exist and it doesn't
  140.    * - WouldBlock: requested I/O operation would block
  141.    * - ConnectionRefused: connection actively refused
  142.    * - ConnectionTimedOut: connection timed out
  143.    * - InProgress: operation (connection) is already in progress
  144.    * - NetFailure: a network failure occurred (no route, host down, host unreachable or similar)
  145.    * - NotSupported: requested operation is not supported
  146.    * - Timeout: a timed operation timed out
  147.    * - UnknownError: an unknown/unexpected error has happened
  148.    * - RemotelyDisconnected: when a connection is disconnected by the other end (since 3.4)
  149.    *
  150.    * @sa error, errorString
  151.    */
  152.   enum SocketError
  153.     {
  154.       NoError = 0,
  155.       LookupFailure,
  156.       AddressInUse,
  157.       AlreadyCreated,
  158.       AlreadyBound,
  159.       AlreadyConnected,
  160.       NotConnected,
  161.       NotBound,
  162.       NotCreated,
  163.       WouldBlock,
  164.       ConnectionRefused,
  165.       ConnectionTimedOut,
  166.       InProgress,
  167.       NetFailure,
  168.       NotSupported,
  169.       Timeout,
  170.       UnknownError,
  171.       RemotelyDisconnected
  172.     };
  173.  
  174. public:
  175.   /**
  176.    * Default constructor.
  177.    */
  178.   KSocketBase();
  179.  
  180.   /**
  181.    * Destructor.
  182.    */
  183.   virtual ~KSocketBase();
  184.  
  185.   /*
  186.    * The following functions are shared by all descended classes and will have
  187.    * to be reimplemented.
  188.    */
  189.  
  190. protected:
  191.   /**
  192.    * Set the given socket options.
  193.    *
  194.    * The default implementation does nothing but store the mask internally.
  195.    * Descended classes must override this function to achieve functionality and
  196.    * must also call this implementation.
  197.    *
  198.    * @param opts    a mask of @ref SocketOptions or-ed bits of options to set
  199.    *            or unset
  200.    * @returns true on success
  201.    * @note this function sets the options corresponding to the bits enabled in @p opts
  202.    *       but will also unset the optiosn corresponding to the bits not set.
  203.    */
  204.   virtual bool setSocketOptions(int opts);
  205.  
  206.   /**
  207.    * Retrieves the socket options that have been set.
  208.    *
  209.    * The default implementation just retrieves the mask from an internal variable.
  210.    * Descended classes may choose to override this function to read the values
  211.    * from the operating system.
  212.    *
  213.    * @returns the mask of the options set
  214.    */
  215.   virtual int socketOptions() const;
  216.  
  217. public:
  218.   /**
  219.    * Sets this socket's blocking mode.
  220.    *
  221.    * In blocking operation, all I/O functions are susceptible to blocking --
  222.    * i.e., will not return unless the I/O can be satisfied. In non-blocking
  223.    * operation, if the I/O would block, the function will return an error
  224.    * and set the corresponding error code.
  225.    *
  226.    * The default implementation toggles the Blocking flag with the current
  227.    * socket options and calls @ref setSocketOptions.
  228.    *
  229.    * @param enable        whether to set this socket to blocking mode
  230.    * @returns whether setting this value was successful; it is NOT the 
  231.    *          final blocking mode.
  232.    */
  233.   virtual bool setBlocking(bool enable);
  234.  
  235.   /**
  236.    * Retrieves this socket's blocking mode.
  237.    *
  238.    * @returns true if this socket is/will be operated in blocking mode,
  239.    *          false if non-blocking.
  240.    */
  241.   bool blocking() const;
  242.  
  243.   /**
  244.    * Sets this socket's address reuseable flag.
  245.    *
  246.    * When the address reuseable flag is active, the address used by
  247.    * this socket is left reuseable for other sockets to bind. If
  248.    * the flag is not active, no other sockets may reuse the same
  249.    * address.
  250.    *
  251.    * The default implementation toggles the AddressReuseable flag with the current
  252.    * socket options and calls @ref setSocketOptions.
  253.    *
  254.    * @param enable        whether to set the flag on or off
  255.    * @returns true if setting this flag was successful
  256.    */
  257.   virtual bool setAddressReuseable(bool enable);
  258.  
  259.   /**
  260.    * Retrieves this socket's address reuseability flag.
  261.    *
  262.    * @returns true if this socket's address can be reused,
  263.    *          false if it can't.
  264.    */
  265.   bool addressReuseable() const;
  266.  
  267.   /**
  268.    * Sets this socket's IPv6 Only flag.
  269.    *
  270.    * When this flag is on, an IPv6 socket will only accept, connect, send to or
  271.    * receive from IPv6 addresses. When it is off, it will also talk to
  272.    * IPv4 addresses through v4-mapped addresses.
  273.    *
  274.    * This option has no effect on non-IPv6 sockets.
  275.    *
  276.    * The default implementation toggles the IPv6Only flag with the current
  277.    * socket options and calls @ref setSocketOptions.
  278.    *
  279.    * @param enable        whether to set the flag on or off
  280.    * @returns true if setting this flag was successful
  281.    */
  282.   virtual bool setIPv6Only(bool enable);
  283.  
  284.   /**
  285.    * Retrieves this socket's IPv6 Only flag.
  286.    *
  287.    * @returns true if this socket will ignore IPv4-compatible and IPv4-mapped
  288.    *          addresses, false if it will accept them.
  289.    */
  290.   bool isIPv6Only() const;
  291.  
  292.   /**
  293.    * Sets this socket Broadcast flag.
  294.    *
  295.    * Datagram-oriented sockets cannot normally send packets to broadcast
  296.    * addresses, nor will they receive packets that were sent to a broadcast
  297.    * address. To do so, you need to enable the Broadcast flag.
  298.    *
  299.    * This option has no effect on stream-oriented sockets.
  300.    *
  301.    * @returns true if setting this flag was successful.
  302.    */
  303.   virtual bool setBroadcast(bool enable);
  304.  
  305.   /**
  306.    * Retrieves this socket's Broadcast flag.
  307.    *
  308.    * @returns true if this socket can send and receive broadcast packets,
  309.    *          false if it can't.
  310.    */
  311.   bool broadcast() const;
  312.  
  313.   /**
  314.    * Retrieves the socket implementation used on this socket.
  315.    *
  316.    * This function creates the device if none has been set
  317.    * using the default factory.
  318.    */
  319.   KSocketDevice* socketDevice() const;
  320.  
  321.   /**
  322.    * Sets the socket implementation to be used on this socket.
  323.    *
  324.    * Note: it is an error to set this if the socket device has
  325.    * already been set once.
  326.    *
  327.    * This function is provided virtual so that derived classes can catch
  328.    * the setting of a device and properly set their own states and internal
  329.    * variables. The parent class must be called.
  330.    *
  331.    * This function is called by @ref socketDevice above when the socket is
  332.    * first created.
  333.    */
  334.   virtual void setSocketDevice(KSocketDevice* device);
  335.  
  336.   /**
  337.    * Sets the internally requested capabilities for a socket device.
  338.    *
  339.    * Most socket classes can use any back-end implementation. However, a few
  340.    * may require specific capabilities not provided in the default
  341.    * implementation. By using this function, derived classes can request
  342.    * that a backend with those capabilities be created when necessary.
  343.    *
  344.    * For the possible flags, see @ref KSocketDevice::Capabilities. However, note
  345.    * that only the Can* flags make sense in this context.
  346.    *
  347.    * @note Since socketDevice must always return a valid backend object, it
  348.    *       is is possible that the created device does not conform to all
  349.    *       requirements requested. Implementations sensitive to this fact
  350.    *       should test the object returned by @ref socketDevice (through
  351.    *       @ref KSocketDevice::capabilities, for instance) the availability.
  352.    *
  353.    * @param add        mask of @ref KSocketDevice::Capabilities to add
  354.    * @param remove    mask of bits to remove from the requirements
  355.    * @return the current mask of requested capabilities
  356.    */
  357.   int setRequestedCapabilities(int add, int remove = 0);
  358.  
  359. protected:
  360.   /**
  361.    * Returns true if the socket device has been initialised in this
  362.    * object, either by calling @ref socketDevice() or @ref setSocketDevice
  363.    */
  364.   bool hasDevice() const;
  365.  
  366.   /**
  367.    * Sets the socket's error code.
  368.    *
  369.    * @param error        the error code
  370.    */
  371.   void setError(SocketError error);
  372.  
  373. public:
  374.   /**
  375.    * Retrieves the socket error code.
  376.    * @sa errorString
  377.    */
  378.   SocketError error() const;
  379.  
  380.   /**
  381.    * Returns the error string corresponding to this error condition.
  382.    */
  383.   inline QString errorString() const
  384.   { return errorString(error()); }
  385.  
  386.   /**
  387.    * Returns the internal mutex for this class.
  388.    *
  389.    * Note on multithreaded use of sockets:
  390.    * the socket classes are thread-safe by design, but you should be aware of
  391.    * problems regarding socket creation, connection and destruction in
  392.    * multi-threaded programs. The classes are guaranteed to work while
  393.    * the socket exists, but it's not wise to call connect in multiple
  394.    * threads. 
  395.    *
  396.    * Also, this mutex must be unlocked before the object is destroyed, which
  397.    * means you cannot use it to guard against other threads accessing the object
  398.    * while destroying it. You must ensure there are no further references to this
  399.    * object when deleting it.
  400.    */
  401.   QMutex* mutex() const;
  402.  
  403. public:
  404.   /**
  405.    * Returns the string describing the given error code, i18n'ed.
  406.    *
  407.    * @param code        the error code
  408.    */
  409.   static QString errorString(SocketError code);
  410.  
  411.   /**
  412.    * Returns true if the given error code is a fatal one, false
  413.    * otherwise. The parameter here is of type int so that
  414.    * casting isn't necessary when using the parameter to signal
  415.    * QClientSocketBase::gotError.
  416.    *
  417.    * @param code        the code to test
  418.    */
  419.   static bool isFatalError(int code);
  420.  
  421. private:
  422.   /// @internal
  423.   /// called by KSocketDevice
  424.   void unsetSocketDevice();
  425.  
  426.   KSocketBase(const KSocketBase&);
  427.   KSocketBase& operator =(const KSocketBase&);
  428.  
  429.   KSocketBasePrivate *d;
  430.  
  431.   friend class KSocketDevice;
  432. };
  433.  
  434. /**
  435.  * @class KActiveSocketBase ksocketbase.h ksocketbase.h
  436.  * @brief Abstract class for active sockets
  437.  *
  438.  * This class provides the standard interfaces for active sockets, i.e.,
  439.  * sockets that are used to connect to external addresses.
  440.  *
  441.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  442.  */
  443. class KDECORE_EXPORT KActiveSocketBase: public QIODevice, virtual public KSocketBase
  444. {
  445. public:
  446.   /**
  447.    * Constructor.
  448.    */
  449.   KActiveSocketBase();
  450.  
  451.   /**
  452.    * Destructor.
  453.    */
  454.   virtual ~KActiveSocketBase();
  455.  
  456.   /**
  457.    * Binds this socket to the given address.
  458.    *
  459.    * The socket will be constructed with the address family,
  460.    * socket type and protocol as those given in the
  461.    * @p address object.
  462.    *
  463.    * @param address        the address to bind to
  464.    * @returns true if the binding was successful, false otherwise
  465.    */
  466.   virtual bool bind(const KResolverEntry& address) = 0;
  467.  
  468.   /**
  469.    * Connect to a remote host.
  470.    *
  471.    * This will make this socket try to connect to the remote host.
  472.    * If the socket is not yet created, it will be created using the
  473.    * address family, socket type and protocol specified in the
  474.    * @p address object.
  475.    *
  476.    * If this function returns with error InProgress, calling it
  477.    * again with the same address after a time will cause it to test
  478.    * if the connection has succeeded in the mean time.
  479.    *
  480.    * @param address        the address to connect to
  481.    * @returns true if the connection was successful or has been successfully
  482.    *          queued; false if an error occurred.
  483.    */
  484.   virtual bool connect(const KResolverEntry& address) = 0;
  485.  
  486.   /**
  487.    * Disconnects this socket from a connection, if possible.
  488.    *
  489.    * If this socket was connected to an endpoint, the connection
  490.    * is severed, but the socket is not closed. If the socket wasn't
  491.    * connected, this function does nothing.
  492.    *
  493.    * If the socket hadn't yet been created, this function does nothing
  494.    * either.
  495.    *
  496.    * Not all socket types can disconnect. Most notably, only
  497.    * connectionless datagram protocols such as UDP support this operation.
  498.    *
  499.    * @return true if the socket is now disconnected or false on error.
  500.    */
  501.   virtual bool disconnect() = 0;
  502.  
  503.   /**
  504.    * This call is not supported on sockets. Reimplemented from QIODevice.
  505.    * This will always return 0.
  506.    */
  507.   virtual Offset size() const
  508.   { return 0; }
  509.  
  510.   /**
  511.    * This call is not supported on sockets. Reimplemented from QIODevice.
  512.    * This will always return 0.
  513.    */
  514.   virtual Offset at() const
  515.   { return 0; }
  516.  
  517.   /**
  518.    * This call is not supported on sockets. Reimplemented from QIODevice.
  519.    * This will always return false.
  520.    */
  521.   virtual bool at(Offset)
  522.   { return false; }
  523.  
  524.   /**
  525.    * This call is not supported on sockets. Reimplemented from QIODevice.
  526.    * This will always return true.
  527.    */
  528.   virtual bool atEnd() const
  529.   { return true; }
  530.  
  531.   /**
  532.    * Returns the number of bytes available for reading without
  533.    * blocking.
  534.    */
  535.   virtual Q_LONG bytesAvailable() const = 0;
  536.  
  537.   /**
  538.    * Waits up to @p msecs for more data to be available on this socket.
  539.    *
  540.    * If msecs is -1, this call will block indefinetely until more data
  541.    * is indeed available; if it's 0, this function returns immediately.
  542.    *
  543.    * If @p timeout is not NULL, this function will set it to indicate
  544.    * if a timeout occurred.
  545.    *
  546.    * @returns the number of bytes available
  547.    */
  548.   virtual Q_LONG waitForMore(int msecs, bool *timeout = 0L) = 0;
  549.  
  550.   /**
  551.    * Reads data from the socket.
  552.    *
  553.    * Reimplemented from QIODevice. See QIODevice::readBlock for
  554.    * more information.
  555.    */
  556.   virtual Q_LONG readBlock(char *data, Q_ULONG len) = 0;
  557.  
  558.   /** @overload
  559.    * Receives data and the source address.
  560.    *
  561.    * This call will read data in the socket and will also
  562.    * place the sender's address in @p from object.
  563.    *
  564.    * @param data        where to write the read data to
  565.    * @param maxlen        the maximum number of bytes to read
  566.    * @param from        the address of the sender will be stored here
  567.    * @returns the actual number of bytes read
  568.    */
  569.   virtual Q_LONG readBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0;
  570.  
  571.   /**
  572.    * Peeks the data in the socket.
  573.    *
  574.    * This call will allow you to peek the data to be received without actually
  575.    * receiving it -- that is, it will be available for further peekings and
  576.    * for the next read call.
  577.    *
  578.    * @param data        where to write the peeked data to
  579.    * @param maxlen        the maximum number of bytes to peek
  580.    * @returns the actual number of bytes copied into @p data
  581.    */
  582.   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen) = 0;
  583.  
  584.   /** @overload
  585.    * Peeks the data in the socket and the source address.
  586.    *
  587.    * This call will allow you to peek the data to be received without actually
  588.    * receiving it -- that is, it will be available for further peekings and
  589.    * for the next read call.
  590.    *
  591.    * @param data        where to write the peeked data to
  592.    * @param maxlen        the maximum number of bytes to peek
  593.    * @param from        the address of the sender will be stored here
  594.    * @returns the actual number of bytes copied into @p data
  595.    */
  596.   virtual Q_LONG peekBlock(char *data, Q_ULONG maxlen, KSocketAddress& from) = 0;
  597.  
  598.   /**
  599.    * Writes the given data to the socket.
  600.    *
  601.    * Reimplemented from QIODevice. See QIODevice::writeBlock for
  602.    * more information.
  603.    */
  604.   virtual Q_LONG writeBlock(const char *data, Q_ULONG len) = 0;
  605.  
  606.   /** @overload
  607.    * Writes the given data to the destination address.
  608.    *
  609.    * Note that not all socket connections allow sending data to different
  610.    * addresses than the one the socket is connected to.
  611.    *
  612.    * @param data        the data to write
  613.    * @param len            the length of the data
  614.    * @param to            the address to send to
  615.    * @returns the number of bytes actually sent
  616.    */
  617.   virtual Q_LONG writeBlock(const char *data, Q_ULONG len, const KSocketAddress& to) = 0;
  618.  
  619.   /**
  620.    * Reads one character from the socket.
  621.    * Reimplementation from QIODevice. See QIODevice::getch for more information.
  622.    */
  623.   virtual int getch();
  624.  
  625.   /**
  626.    * Writes one character to the socket.
  627.    * Reimplementation from QIODevice. See QIODevice::putch for more information.
  628.    */
  629.   virtual int putch(int ch);
  630.  
  631.   /**
  632.    * This call is not supported on sockets. Reimplemented from QIODevice.
  633.    * This will always return -1;
  634.    */
  635.   virtual int ungetch(int)
  636.   { return -1; }
  637.  
  638.   /**
  639.    * Returns this socket's local address.
  640.    */
  641.   virtual KSocketAddress localAddress() const = 0;
  642.  
  643.   /**
  644.    * Return this socket's peer address, if we are connected.
  645.    * If the address cannot be retrieved, the returned object will contain
  646.    * an invalid address.
  647.    */
  648.   virtual KSocketAddress peerAddress() const = 0;
  649.  
  650.   // FIXME KDE 4.0:
  651.   // enable this function
  652. #if 0
  653.   /**
  654.    * Returns this socket's externally-visible address, if known.
  655.    */
  656.   virtual KSocketAddress externalAddress() const = 0;
  657. #endif
  658.  
  659. protected:
  660.   /**
  661.    * Sets the socket's error code and the I/O Device's status.
  662.    *
  663.    * @param status        the I/O Device status
  664.    * @param error        the error code
  665.    */
  666.   void setError(int status, SocketError error);
  667.  
  668.   /**
  669.    * Resets the socket error code and the I/O Device's status.
  670.    */
  671.   void resetError();
  672. };
  673.  
  674. /**
  675.  * @class KPassiveSocketBase ksocketbase.h ksocketbase.h
  676.  * @brief Abstract base class for passive sockets.
  677.  *
  678.  * This socket provides the initial functionality for passive sockets,
  679.  * i.e., sockets that accept incoming connections.
  680.  *
  681.  * @author Thiago Macieira <thiago.macieira@kdemail.net>
  682.  */
  683. class KDECORE_EXPORT KPassiveSocketBase: virtual public KSocketBase
  684. {
  685. public:
  686.   /**
  687.    * Constructor
  688.    */
  689.   KPassiveSocketBase();
  690.  
  691.   /**
  692.    * Destructor
  693.    */
  694.   virtual ~KPassiveSocketBase();
  695.  
  696.   /**
  697.    * Binds this socket to the given address.
  698.    *
  699.    * The socket will be constructed with the address family,
  700.    * socket type and protocol as those given in the
  701.    * @p address object.
  702.    *
  703.    * @param address        the address to bind to
  704.    * @returns true if the binding was successful, false otherwise
  705.    */
  706.   virtual bool bind(const KResolverEntry& address) = 0;
  707.  
  708.   /**
  709.    * Puts this socket into listening mode.
  710.    *
  711.    * Placing a socket in listening mode means that it will
  712.    * be allowed to receive incoming connections from
  713.    * remote hosts.
  714.    *
  715.    * Note that some socket types or protocols cannot be
  716.    * put in listening mode.
  717.    *
  718.    * @param backlog        the number of accepted connections to
  719.    *                hold before starting to refuse
  720.    * @returns true if the socket is now in listening mode
  721.    */
  722.   virtual bool listen(int backlog) = 0;
  723.  
  724.   /**
  725.    * Closes this socket. All resources used are freed. Note that closing
  726.    * a passive socket does not close the connections accepted with it.
  727.    */
  728.   virtual void close() = 0;
  729.  
  730.   /**
  731.    * Accepts a new incoming connection.
  732.    *
  733.    * If this socket was in listening mode, you can call this
  734.    * function to accept an incoming connection.
  735.    *
  736.    * If this function cannot accept a new connection (either
  737.    * because it is not listening for one or because the operation
  738.    * would block), it will return NULL.
  739.    *
  740.    * Also note that descended classes will override this function
  741.    * to return specialised socket classes.
  742.    */
  743.   virtual KActiveSocketBase* accept() = 0;
  744.  
  745.   /**
  746.    * Returns this socket's local address.
  747.    */
  748.   virtual KSocketAddress localAddress() const = 0;
  749.  
  750.   /**
  751.    * Returns this socket's externally-visible address if known.
  752.    */
  753.   virtual KSocketAddress externalAddress() const = 0;
  754.  
  755. private:
  756.   KPassiveSocketBase(const KPassiveSocketBase&);
  757.   KPassiveSocketBase& operator = (const KPassiveSocketBase&);
  758. };
  759.  
  760. }                // namespace KNetwork
  761.  
  762. #endif
  763.